package com.jstyle.jclife.view;



import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.jstyle.jclife.R;
import com.jstyle.jclife.utils.DateUtil;
import com.jstyle.jclife.utils.ResolveData;
import com.jstyle.jclife.utils.ScreenUtils;

/**
 * 圆形进度条
 * 
 * @author Administrator
 *
 */
public class SleepCircleProgress extends View {
	private Paint progressPaint;//进度条画笔
	private int bigSqure;//进度条圆半径
	private int strokeWidth;

	private RectF rectF;
	private float endAngel = 50;
	private float SweepAngel = 270;
	float startAngle=135;
	private int width, height;
	private Paint ballPaint;//小球画笔
	private int textSize,smallTextSize,normalTextSize;
	private Paint bgCiclePaint;//背景圆画笔
	Paint textPaint;
	int goBed=-1;
	int upBed=-1;
	String goBedString="--:--";
	String upBedString="--:--";
	int[]sleepData;

	private int defaultWidth;
	private int defaultHeight;
	private float circleRadius;
	private Bitmap stepBitmap;
	int lightColor,deepColor,remColor,awakeColor,upBedColor;
	public SleepCircleProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.mCicleProgress);
		width = ScreenUtils.getScreenWidth(context) / 2;
		bigSqure = ScreenUtils.dip2px(context, 75);
		circleRadius =ScreenUtils.dip2px(context, 4);
		height = ScreenUtils.dip2px(context, 30) ;
		strokeWidth = ScreenUtils.dip2px(context, 12);
		defaultHeight = ScreenUtils.dip2px(context, 130);
		defaultWidth = ScreenUtils.dip2px(context, 130);
		textSize = ScreenUtils.dip2px(context, 24);
		normalTextSize = ScreenUtils.dip2px(context, 15);
		smallTextSize = ScreenUtils.dip2px(context, 12);
		stepBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.home_sleep);
		lightColor=getResources().getColor(R.color.week_sleep_light);
		deepColor=getResources().getColor(R.color.week_sleep_deep_chart);
		//remColor=getResources().getColor(R.color.sleep_rem_color);
		awakeColor=getResources().getColor(R.color.week_sleep_awake);
		upBedColor=Color.parseColor("#c8c7cc");
		init();
	}

	private void init() {
		// TODO Auto-generated method stub

		rectF = new RectF();
		progressPaint = new Paint();
		progressPaint.setColor(getResources().getColor(
				R.color.progress_color));
		progressPaint.setStyle(Paint.Style.STROKE);
		progressPaint.setAntiAlias(true);
		progressPaint.setStrokeWidth(strokeWidth);
		progressPaint.setStrokeCap(Paint.Cap.ROUND);



		bgCiclePaint = new Paint();
		bgCiclePaint.setColor(Color.parseColor("#a8a8a8"));
		bgCiclePaint.setAntiAlias(true);
		bgCiclePaint.setStyle(Paint.Style.STROKE);
		bgCiclePaint.setStrokeWidth(strokeWidth);
		bgCiclePaint.setStrokeCap(Paint.Cap.ROUND);

		ballPaint=new Paint();
		ballPaint.setColor(Color.parseColor("#e0e0df"));
		ballPaint.setAntiAlias(true);

		textPaint=new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(textSize);
	}

	private String topTxt = "步数";
	private String bottomTxt = "0";

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		rectF.set(0+strokeWidth, 0+strokeWidth, getWidth()-strokeWidth, getHeight()-strokeWidth);
		drawBgArc(canvas);
		drawProgress(canvas);
		drawCircle(canvas);
		drawText(canvas);
		drawTextRight(canvas);
		drawBitMap(canvas);
	}

	private void drawBitMap(Canvas canvas) {
		float x=getWidth()/2-stepBitmap.getWidth()/2;
		float y=getHeight()/2-stepBitmap.getHeight()/2-strokeWidth/2;
		canvas.drawBitmap(stepBitmap,x,y,textPaint);
	}

	private void drawText(Canvas canvas) {
		float x = 0;
		float y = 0;
		float sin = (float) Math.sin(15 * Math.PI / 180);
		float cos = (float) Math.cos(15 * Math.PI / 180);
		float centerX=getWidth()/2;
		float centerY=getHeight()/2;
		x = sin * (centerX-strokeWidth);
		y = cos * (centerX-strokeWidth);
		textPaint.setTextSize(normalTextSize);
		Rect rect = new Rect();
		String stepValue=goBedString;
		textPaint.getTextBounds(stepValue, 0, stepValue.length(), rect);
		int height = rect.height();
		int width=rect.width();
		canvas.drawText(stepValue,centerX-x-width,centerY+y+height,textPaint);
	}

	private static final String TAG = "SleepCircleProgress";
	private void drawTextRight(Canvas canvas) {
		float x = 0;
		float y = 0;
		float sin = (float) Math.sin(345 * Math.PI / 180);
		float cos = (float) Math.cos(345 * Math.PI / 180);
		float centerX=getWidth()/2;
		float centerY=getHeight()/2;
		x = sin * (centerX-strokeWidth);
		y = cos * (centerX-strokeWidth);
		textPaint.setTextSize(normalTextSize);
		Rect rect = new Rect();
		String stepValue=upBedString;
		textPaint.getTextBounds(stepValue, 0, stepValue.length(), rect);
		int height = rect.height();
		int width=rect.width();

		canvas.drawText(stepValue,centerX+x+width,centerY+y+height,textPaint);
	}

	/**先画首尾保证首尾的圆
	 * @param canvas
	 */
	private void drawProgress(Canvas canvas) {
		if(sleepData==null||upBed==-1||goBed==-1)return;
		int color=upBedColor;
		float startSleepAngel=startAngle;
		float angel=(SweepAngel)/(upBed-goBed+1);
		progressPaint.setColor(getSleepColor(goBed));
		progressPaint.setStrokeCap(Paint.Cap.ROUND);
		canvas.drawArc(rectF, startSleepAngel, angel, false, progressPaint);
		progressPaint.setColor(getSleepColor(upBed));
		canvas.drawArc(rectF, startAngle+SweepAngel-angel, angel, false, progressPaint);
		startSleepAngel+=10;
		for(int i=upBed-1;i>goBed;i--){
			progressPaint.setColor(getSleepColor(i));
			progressPaint.setStrokeCap( Paint.Cap.BUTT);
			canvas.drawArc(rectF, startSleepAngel, SweepAngel-(upBed-i)*angel, false, progressPaint);
		}

	}

	private int getSleepColor(int position){
		int color=upBedColor;
		if(sleepData[position]==-1)return color;
		int sleepLevel= ResolveData.getSleepLevel(sleepData[position]);
		if(sleepLevel==0){
			color=deepColor;
		}else if(sleepLevel==1){
			color=lightColor;
		}else if(sleepLevel==2){
			color=awakeColor;
		}
		return color;
	}
	private void drawBgArc(Canvas canvas) {
		canvas.drawArc(rectF, startAngle, SweepAngel, false, bgCiclePaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
			height = defaultHeight;
		}
		if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
			width = defaultWidth;
		}
		setMeasuredDimension(width, height);

	}

	private void drawCircle(Canvas canvas) {
		// TODO Auto-generated method stub
		float x = 0;
		float y = 0;
		float sin = (float) Math.sin(46 * Math.PI / 180);
		float cos = (float) Math.cos(46 * Math.PI / 180);
		float centerX=getWidth()/2;
		float centerY=getHeight()/2;
		x = sin * (centerX-strokeWidth);
		y = cos * (centerX-strokeWidth);
		canvas.drawCircle(centerX - x,   centerY+y, circleRadius, ballPaint);
	}

	public void setProgress(float progress) {
		endAngel = SweepAngel* progress;

		postInvalidate();
	}
	public void setSleepTime(int[] sleepData,int goBed,int upBed) {
		this.sleepData=sleepData;
		this.goBed=goBed;
		this.upBed=upBed;
		this.goBedString= goBed==-1?"--:--":DateUtil.getCountTime(goBed);
		this.upBedString=upBed==-1 ?"--:--":DateUtil.getCountTime(upBed);
		postInvalidate();
	}

	private float start = 0.0f;

	public void startAnim(float end) {
		if (start == end)
			return;
		ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progress",
				start, end);
		start = end;

		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(800);
		animatorSet.setInterpolator(new DecelerateInterpolator());
		animatorSet.setTarget(this);
		animatorSet.play(animator);
		animatorSet.start();

	}

	public void setBottomText(int step) {
		this.bottomTxt = String.valueOf(step);
		postInvalidate();
	}

	public void update(int step, float progress) {

		this.bottomTxt = String.valueOf(step);

		endAngel = SweepAngel * progress;
		postInvalidate();
	}

//	public void changeCicleColor(int color) {
//		progressPaint.setColor(color);
//		ballPaint.setColor(color);
//		ballPaint.setShadowLayer(blurWidth, 0, 0, color);
//		postInvalidate();
//	}

	public void update(int sleepTime, String bottomString, String topString) {
		this.bottomTxt = bottomString;
		this.topTxt = topString;
		postInvalidate();
	}

	
}
